package model;

import java.util.Iterator;
import java.util.LinkedList;

import util.Utils;

public class XMLList implements Iterable<XML>{

	private LinkedList<XML> source;
	
	public XMLList() {
		source = new LinkedList<XML>();
	}

	public LinkedList<XML> getSource() {
		return source;
	}
	
	public void add(XML curNode) {
		source.add(curNode);		
	}
	
	public int length(){
		return source.size();
	}

	@Override
	public String toString() {
		return getFormatedChildren(source, 0);
	}
	
	
	private String getFormatedChildren(LinkedList<XML> childs, int level) {
		String result = ""; 
		for (XML node : childs) {
			result += Utils.formatNode(node, level + 1, false);
			if ( node.hasChildren() ) {
				result += getFormatedChildren(node.getChildrenList(), level + 1); // Recursively
			}
			result += Utils.formatNode(node, level + 1, true);
		}
		return result;
	}

	@Override
	public Iterator<XML> iterator() {		
		return source.iterator();
	}

	
}
